package enumandlambda;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shkstart
 * @create 2022-11-27-17:16
 */
public class LambdaTest {
    public static void main(String[] args) {
//        List<String> list = Arrays.asList("java", "Msyql", "python");
//
//        //传统的循环打印方法
//        for(String item : list){
//            System.out.println(item);
//        }
//
//        //lambda
//        list.forEach( (item ) -> {
//            System.out.println(item);
//        });
//        //简洁版lambda
//        list.forEach(item -> System.out.println(item));

//        mapTest();

//        listSortTest();

        List<String> list = Arrays.asList("java", "lambda", "Spring", "lambda", "springboot");
        //1.8之前
//      for(String item : list){
//          if(item.equals("lambda")){
//              System.out.println("存在");
//          }
//      }
        //lambda
        List<String> finds = list.stream().filter(
                s -> s.equals("lambda")).collect(Collectors.toList());
        System.out.println(finds);


    }

    /**
     * list排序
     */
    private static void listSortTest() {
        List<Integer> list = Arrays.asList(5,7,2,9);
        System.out.println("排序前"+ list);

        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        System.out.println("排序后"+list);
        list.sort((o1 ,o2)-> o2 - o1
        );//    ->{ return 02 - o2 ;}
        System.out.println("排序后"+list);
    }

    /**
     * map循环使用实例
     */
    private static void mapTest() {
        HashMap<String , String> map = new HashMap<String , String>(){{
            put("java","java Value");
            put("mysql", "mysql");
        }};
        //jdk1.8之前
        for (Map.Entry<String,String> item:map.entrySet()) {
            System.out.println(item.getKey()+":"+item.getValue());
        }
        //lambda
        map.forEach((k,v)-> System.out.println(k+":"+v));
    }
}
